<%@ page import="java.net.URLEncoder" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2016/4/6
  Time: 11:49
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%!
    boolean isNull(String str){
        return str == null || str.trim().length() == 0;
    }
%>
<%
    request.setCharacterEncoding("UTF-8");
    if("POST".equals(request.getMethod())){
        String name = request.getParameter("name");
        String value = request.getParameter("value");
        String maxAge = request.getParameter("maxAge");
        String domain = request.getParameter("domain");
        String path = request.getParameter("path");
        String comment = request.getParameter("comment");
        String secure = request.getParameter("secure");

        if(!isNull(name)) {
            Cookie cookie = new Cookie(URLEncoder.encode(name, "UTF-8"),
                    URLEncoder.encode(value, "UTF-8"));
            if(!isNull(maxAge)) cookie.setMaxAge(Integer.parseInt(maxAge));
            if(!isNull(domain)) cookie.setDomain(domain);
            if(!isNull(path)) cookie.setPath(path);
            if(!isNull(comment)) cookie.setComment(comment);
            if(!isNull(secure)) cookie.setSecure("true".equalsIgnoreCase(secure));

            response.addCookie(cookie);
        }
    }
%>
<html>
<head>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <title>setCookie</title>
    <style type="text/css">
        fieldset{
            width:60%;
            margin: 0 auto;
        }
        div{
            width:80%;
            margin:0 auto;
        }
        input{
            width:100%;
        }
        td{
            text-align: right;
        }
        select{
            width: 100%;
        }
    </style>

</head>
<body>
<div>
    <fieldset>
        <legend>当前有效的cookie信息</legend>
        <script type="text/javascript">
            document.write(document.cookie);
        </script>
    </fieldset>
    <fieldset>
        <legend>设置新的cookie信息</legend>
        <form action="setCookie.jsp" method="post">
            <table align="center">
                <tr>
                    <td>name:</td>
                    <td><input name="name" type="text"/> </td>
                </tr>
                <tr>
                    <td>value:</td>
                    <td><input name="value" type="text"/> </td>
                </tr>
                <tr>
                    <td>maxAge:</td>
                    <td>
                        <select name="maxAge">
                            <option value="-1">临时cookie(-1)</option>
                            <option value="0">删除该cookie(0)</option>
                            <option value="3600">cookie 1 hour</option>
                            <option value="2592000">cookie 1 months</option>
                            <option value="31104000">cookie 1 years</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>domain:</td>
                    <td><input name="domain" type="text"/> </td>
                </tr>
                <tr>
                    <td>path:</td>
                    <td><input name="path" type="text"/> </td>
                </tr>
                <tr>
                    <td>comment:</td>
                    <td><input name="domain" type="text"/> </td>
                </tr>
                <tr>
                    <td>secure:</td>
                    <td><input name="secure" type="text"/> </td>
                </tr>
                <tr>
                    <td><input type="button" value="refresh" onclick="location='setCookie.jsp'" /></td>
                    <td><input type="submit" value="submit"/> </td>
                </tr>
            </table>
        </form>
    </fieldset>
</div>

</body>
</html>
